﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

using System;

namespace Checkers.Common
{
    public class Player
    {
        public event EventHandler ReadyToMoveEvent;
        public event EventHandler MoveCompleteEvent;

        Checker[] mCheckers = new Checker[12];

        public Player(CheckerColor color)
        {
            mColor = color;

            for (int n = 0; n < 12; n++)
            {
                mCheckers[n] = new Checker(n, color);
            }
        }

        public Checker GetChecker(int n)
        {
            if ((n < 0) || (n >= 12)) return null;
            return mCheckers[n];
        }

        public Player Next { get; set; }

        CheckerColor mColor;

        public CheckerColor Color { get { return mColor; } }

        public bool IsRobot { get; set; }

        MoveList mMoveList = new MoveList();

        public MoveList MoveList { get { return mMoveList; } }

        public void StartTurn()
        {
            mMoveList.Clear();

            if (CanJump())
            {
                foreach (Checker checker in mCheckers)
                {
                    mMoveList.Append(checker.GetJumpList());
                }
            }
            else
            {
                foreach (Checker checker in mCheckers)
                {
                    mMoveList.Append(checker.GetMoveList());
                }
            }

            if (mMoveList.Count > 0)
            {
                if (ReadyToMoveEvent != null)
                {
                    ReadyToMoveEvent(this, null);
                }
            }
        }

        public void EndTurn()
        {
            mMoveList.Clear();
        }

        public void Reset()
        {
            mMoveList.Clear();

            SetupPieces();
        }

        public void ApplyMove(Move move)
        {
            mMoveList.Clear();
            
            Checker checker = move.Tile1.Checker;

            checker.MoveCompleteEvent += new System.EventHandler(OnMoveComplete);

            checker.MoveTo(move.Tile2);

            if (move.Jump != null)
            {
                move.Jump.Checker.Capture();

                if (!checker.IsGoingToBeCrowned())
                {
                    mMoveList = checker.GetJumpList();
                }
            }
        }

        void OnMoveComplete(object sender, System.EventArgs e)
        {
            Checker checker = (Checker)sender;

            checker.MoveCompleteEvent -= new EventHandler(OnMoveComplete);

            if (checker.IsGoingToBeCrowned())
            {
                checker.IsKing = true;
            }

            if (MoveCompleteEvent != null)
            {
                MoveCompleteEvent(this, null);
            }

            if (mMoveList.Count > 0)
            {
                if (ReadyToMoveEvent != null)
                {
                    ReadyToMoveEvent(this, null);
                }
            }
        }

        public bool CanJump()
        {
            bool canJump = false;

            foreach (Checker checker in mCheckers)
            {
                if (checker.Color == Color)
                {
                    if (checker.CanJump())
                    {
                        canJump = true;
                    }
                }
            }

            return canJump;
        }

        public bool CanMove()
        {
            bool canMove = false;

            foreach (Checker checker in mCheckers)
            {
                if (checker.Color == Color)
                {
                    if (checker.CanMove())
                    {
                        canMove = true;
                    }
                }
            }

            return canMove;
        }

        public void RemovePieces()
        {
            foreach (Checker checker in mCheckers)
            {
                checker.MoveTo(null);
            }
        }

        public void SetupPieces()
        {
            foreach (Checker checker in mCheckers)
            {
                checker.IsKing = false;
                checker.MoveTo(checker.StartTile);
            }
        }
    }
}
